﻿using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace com.IronOne.BoardPACWinApp.View.UserControl
{
    public sealed partial class InputMessageDialog
    {
        private static TaskCompletionSource<bool> _mTaskCompletionSource;
        private readonly DispatcherTimer _dispatcherTimer;
        private const int Duration = 30; //In seconds
        private readonly Visibility _globalCancelBtn;
        private readonly Visibility _globalOkBtn;

        public InputMessageDialog()
        {
            InitializeComponent();
        }

        public InputMessageDialog(string label, Visibility okBtn, Visibility cancelBtn, bool isAutoCancelOn)
        {
            try
            {
                InitializeComponent();

                Label = label;
                _globalOkBtn = okBtn;
                _globalCancelBtn = cancelBtn;

                if (!isAutoCancelOn) return;

                _dispatcherTimer = new DispatcherTimer();
                _dispatcherTimer.Tick += (s, args) => DispatcherTimer_Tick();
                _dispatcherTimer.Interval = new TimeSpan(0, 0, Duration);
                _dispatcherTimer.Start();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public string Label { get; set; }

        private void DispatcherTimer_Tick()
        {
            try
            {
                _dispatcherTimer.Stop();
                CancelBtn.Visibility = Visibility.Visible;
                MRect2.Height = MRect2.Height + 30;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Task<bool> ShowAsync()
        {
            try
            {
                InitFields();
                MPopup.IsOpen = true;
                MPopup.IsLightDismissEnabled = false;
                _mTaskCompletionSource = new TaskCompletionSource<bool>();
                return _mTaskCompletionSource.Task;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void HideAsync()
        {
            try
            {
                MPopup.IsOpen = false;
                if (!_mTaskCompletionSource.Task.IsCompleted)
                    _mTaskCompletionSource.SetResult(true);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void InitFields()
        {
            try
            {
                MRect1.Height = Window.Current.Bounds.Height;
                MRect1.Width = Window.Current.Bounds.Width;

                MRect2.Width = Window.Current.Bounds.Width;
                MRect2.Height = Window.Current.Bounds.Height * 0.15;

                //m_TextBox.Width = Window.Current.Bounds.Width / 2;
                //m_TextBlock1.Text = Label;

                BtnStkPnl.Margin = new Thickness((Window.Current.Bounds.Width * 0.3), 0, 0, 0);

                if (!String.IsNullOrWhiteSpace(Label))
                    MTextBlock2.Text = Label;

                OkBtn.Visibility = _globalOkBtn;
                CancelBtn.Visibility = _globalCancelBtn;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /*public TextBox TextBox
        {
            get { return m_TextBox; }
        }*/

        private void OkClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                _mTaskCompletionSource.SetResult(true);
                MPopup.IsOpen = false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void CancelClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                _mTaskCompletionSource.SetResult(false);
                MPopup.IsOpen = false;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}